package com.greateamd.timetableasy.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import net.sf.gilead.pojo.gwt.LightEntity;

@SuppressWarnings("serial")
@Entity
@Table(name="promotions")
public class Promotion extends LightEntity implements PersistableEntity,HasEventsEntity,Serializable
{

	@Id
	@GeneratedValue
	private Integer id;
	private String name;
	private boolean enabled = true;

	@OneToMany(mappedBy="promotion", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
	private Set<User> users;

	@OneToMany(mappedBy="promotion", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
	private Set<Course> courses;
	
	@OneToMany(fetch=FetchType.EAGER)
	private Set<StudyPeriod> studyPeriods;
	
	@ManyToOne
	@JoinColumn(name="id_campus", referencedColumnName="id")
	private Campus campus;
	
	@ManyToMany(fetch = FetchType.LAZY,targetEntity=Event.class,cascade=CascadeType.ALL)
    @JoinTable(name="promotions_events",
		        joinColumns = @JoinColumn(columnDefinition="promotion"),
		        inverseJoinColumns=@JoinColumn(name="event_id"))
	private Set<Event> events;
	
	public Promotion()
	{}

	public Promotion(String name,Campus campus) 
	{
		this.name = name;
		this.campus = campus;
	}


	public Set<Course> getCourses() {
		return courses;
	}

	public void setCourses(Set<Course> courses) {
		this.courses = courses;
	}

	public Campus getCampus() {
		return campus;
	}

	public void setCampus(Campus campus) {
		this.campus = campus;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() 
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public boolean isEnabled() 
	{
		return enabled;
	}

	public void setEnabled(boolean enabled)
	{
		this.enabled = enabled;
	}

	public Set<User> getUsers() {
		return users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}

	private Set<StudyPeriod> getStudyPeriods() {
		return studyPeriods;
	}

	public Set<Event> getEvents() {
		return events;
	}

	private void setStudyPeriods(Set<StudyPeriod> studyPeriods) {
		this.studyPeriods = studyPeriods;
	}

	private void setEvents(Set<Event> events) {
		this.events = events;
	}

	public void addEvent(Event event) {
		if (events == null)
			events = new HashSet<Event>();
		this.events.add(event);
	}
	
	public void addStudyPeriod(StudyPeriod sp) {
		if (studyPeriods == null)
			studyPeriods = new HashSet<StudyPeriod>();
		this.studyPeriods.add(sp);
	}
	
	public void addCourse(Course course) {
		if (courses == null)
			courses = new HashSet<Course>();
		this.courses.add(course);
	}
	
	public void addUser(User user) {
		if (users == null)
			users = new HashSet<User>();
		this.users.add(user);
	}
	
	@Override
	public String toString() {
		return getName();
	}
	
	@Override
	public boolean equals(Object obj) {
		if( obj != null && obj instanceof Promotion ){
			Promotion p = (Promotion)obj;
			if(p.getId()==null){
				return false;
			} else if(p.getId().equals(getId())){
				return true;
			}
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		if(getId()==null)
			return super.hashCode();
		else
			return 16*getId().hashCode();
	}
}
